﻿using System;
using System.Collections.Generic;
using System.Text;
using Chaliy.DbPro.Generators.TextAuthoring;
using Chaliy.DbPro.Generators.TextAuthoring.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;

namespace Chaliy.DbPro.Generators.Tests.TextAuthoring
{
    internal static class TermAccessorUtilities
    {

		private static readonly string[] DefaultSeparators = new string[]
		{
			".", "...", "?"
		};

		private static readonly string[] DefaultComas = new string[]
		{
			",", ";"
		};

		private static readonly string[] ThreeWordSamples = new string[]
		{
			"Rob", "Bob", "Dob"
		};

		private static readonly string[] SingleWordSample = new string[]
		{
			"Rob"
		};

        #region Methods

        public static ITermAccessor Create(List<string> terms)
        {
            List<TermConfiguration> termList = terms.ConvertAll<TermConfiguration>(
                TermAccessorUtilities.ConvertToConfiguration
                );

            return new ListTermAccessor(termList);
        }

		public static ITermAccessor Create(params string[] terms)
		{
			List<TermConfiguration> termList = new List<string>(terms).ConvertAll<TermConfiguration>(
				TermAccessorUtilities.ConvertToConfiguration
				);

			return new ListTermAccessor(termList);
		}

        private static TermConfiguration ConvertToConfiguration(string term)
        {
            return new TermConfiguration { Value = term };
        }

		public static ITermAccessor CreateWithThreeWordSamples()
		{
			return TermAccessorUtilities.Create(TermAccessorUtilities.ThreeWordSamples);
		}

		public static ITermAccessor CreateWithSingleWordSample()
		{
			return TermAccessorUtilities.Create(TermAccessorUtilities.SingleWordSample);
		}		

		public static ITermAccessor CreateWithDefaultSeparators()
		{
			return TermAccessorUtilities.Create(TermAccessorUtilities.DefaultSeparators);
		}

		public static ITermAccessor CreateWithEmpty()
		{
			return TermAccessorUtilities.Create();
		}

		public static ITermAccessor CreateWithDefaultComas()
		{
			return TermAccessorUtilities.Create(TermAccessorUtilities.DefaultComas);
		}

		private static bool CheckIfTextContainsSomething(string text, ITermAccessor accessor)
		{
			foreach (TermConfiguration term in accessor)
			{
				if (text.Contains(term.Value))
				{
					return true;
				}
			}
			return false;
		}

		private static string CreateDescriptionList(ITermAccessor accessor)
		{
			StringBuilder builder = new StringBuilder();

			foreach (TermConfiguration term in accessor)
			{
				if (builder.Length > 0)
				{
					builder.Append(", ");
				}
				builder.Append(term.Value);
			}

			return builder.ToString();
		}

		public static void AssertTextContainsSomething(string result, ITermAccessor accessor)
		{
			Assert.IsTrue(
				TermAccessorUtilities.CheckIfTextContainsSomething(result, accessor),
				String.Format(
					CultureInfo.InvariantCulture,
					"Must contain something from the list of the terms. Terms expected \"{0}\". Input \"{1}\"",
					TermAccessorUtilities.CreateDescriptionList(accessor), result
					)
				);
		}

		private static bool CheckIfStringExistsWithinAccessor(string term, ITermAccessor accessor)
		{
			foreach (TermConfiguration termConfiguration in accessor)
			{
				if (term.Equals(termConfiguration.Value))
				{
					return true;
				}
			}
			return false;
		}

		public static void AssertStringExistsWithinAccessor(string term, ITermAccessor accessor)
		{					
			Assert.IsTrue(
				TermAccessorUtilities.CheckIfStringExistsWithinAccessor(term, accessor),
				String.Format(
					CultureInfo.InvariantCulture,
					"Term may exists within accessor. Terms existed \"{0}\". Term \"{1}\"",
					TermAccessorUtilities.CreateDescriptionList(accessor), term
					)
				);
		}

        #endregion
    }
}
